/*
 * Created: Author: Aléx Carvalho / Date: Feb 23, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-12 03:38:01 +0000 (Fri, 12 Jun 2015) $
 */
package com.cpm;

import com.cpm.business.SemaphoreColor;
import com.cpm.entities.Competence;
import com.cpm.entities.Folder;
import com.cpm.entities.Indicator;
import com.cpm.entities.StrategicPlan;
import com.cpm.view.CSSClass;
import com.cpm.wrapper.Performance;

public class SemaphoreColorImpl implements SemaphoreColor {

    @Override
    public CSSClass getSemaphoreColorToStrategicPlan(StrategicPlan plan) {
        return getSemaphoreColor(plan.getPerformance(), plan);
    }

    @Override
    public CSSClass getSemaphoreColorToFolder(Folder f) {
        Performance performance = f.getPerformance();

        return getSemaphoreColor(performance, f.getStrategicPlan());
    }
    
    @Override
    public CSSClass getSemaphoreColorToIndicator(Indicator i) {
        Performance performance = i.getPerformance();

        return getSemaphoreColor(performance, i.getStrategicPlan());
    }
    
    @Override
    public CSSClass getSemaphoreColorToCompetence(Competence c) {
        Performance performance = c.getPerformance();

        return getSemaphoreColor(performance, c.getIndicator().getStrategicPlan());
    }

    private CSSClass getSemaphoreColor(Performance performance, StrategicPlan plan) {

        if (performance == null || performance.getValue() == null) {
            return CSSClass.SEMAPHORE_WHITE;
        }

        Float value = performance.getValue();
        Short low = plan.getLimitLow().getValue();
        Short mid = plan.getLimitMid().getValue();
        Short high = plan.getLimitHigh().getValue();

        if (value < low) {
            return CSSClass.SEMAPHORE_RED;
        }

        if (value >= low && value <= mid) {
            return CSSClass.SEMAPHORE_YELLOW;
        }

        if (value > mid && value <= high) {
            return CSSClass.SEMAPHORE_GREEN;
        }

        return CSSClass.SEMAPHORE_BLUE;
    }
}
